﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Windows.Forms;

namespace TNCAddIn
{
    class Opration
    {
        public void updateAll(Outlook.Items items, String mail)
        {
            Object item = items.GetNext();
            while (item != null) {
                Outlook.AppointmentItem appointment = item as Outlook.AppointmentItem;
                if (appointment != null) {
                    try { 
                        update(appointment, mail); 
                    }
                    catch (Exception e) {
                        throw e;
                    }
                }
            }
            /*foreach (Outlook.AppointmentItem item in items)
            {
                try
                {
                    update(item, mail);
                }
                catch(Exception e)
                {
                    throw e;
                }
            }*/
        }

        private void update(Outlook.AppointmentItem item, String mail) 
        {
            //MessageBox.Show(item.ToString());
            if (!item.AllDayEvent /*全天事件不抓取*/
                && (item.UserProperties["sync"] == null || item.UserProperties["sync"].Value == "no"))
            {
                if (item.Organizer != item.Application.Session.CurrentUser.Name
                    && item.ResponseRequested
                    && item.ResponseStatus != Outlook.OlResponseStatus.olResponseAccepted) {
                    // 不抓取未响应的会议邀请
                    return;
                }
                TNCAppointment tnc_appt = new TNCAppointment();
                try
                {
                    tnc_appt.Subject = item.Subject;
                    tnc_appt.Body = item.Body;
                    tnc_appt.ID = item.GlobalAppointmentID;
                    tnc_appt.Location = item.Location;
                    tnc_appt.StartDay = item.Start.ToString("yyyy-MM-dd");
                    tnc_appt.StartTime = item.Start.ToString("HH:mm:ss");
                    tnc_appt.EndDay = item.End.ToString("yyyy-MM-dd");
                    tnc_appt.EndTime = item.End.ToString("HH:mm:ss");
                    if (item.UserProperties["buget"] != null)
                    {
                        tnc_appt.Buget = item.UserProperties["buget"].Value;
                    }
                    else {
                        tnc_appt.Buget = Properties.Settings.Default.DefaultBudgetCenter;
                    }
                    tnc_appt.Time = DateTime.Now.ToString();

                    if (tnc_appt.Buget == "私人事件") {
                        // 不抓取私人事件
                        return;
                    }

                } catch (Exception e) {
                    //MessageBox.Show(e.Message);
                }
                String paths = "";

                try
                {
                    foreach (Outlook.Attachment att in item.Attachments)
                    {
                        att.SaveAsFile(Properties.Settings.Default.TempDir + att.FileName);
                        if (paths != "")
                            paths += ",";
                        paths += Properties.Settings.Default.TempDir + att.FileName;
                    }
                } catch (Exception e) {
                    //MessageBox.Show(e.Message);
                }

                DataContractJsonSerializer json = new DataContractJsonSerializer(tnc_appt.GetType());
                string szJson = "";
                using (MemoryStream stream = new MemoryStream())
                {
                    json.WriteObject(stream, tnc_appt);
                    szJson = UTF8Encoding.UTF8.GetString(stream.ToArray());
                    stream.Close();
                }

                HttpClient hc = new HttpClient();
                try
                {
                    //MessageBox.Show("Auth Upload: " + szJson);
                    hc.AuthUpload(mail, szJson, paths);
                    if (item.UserProperties["sync"] == null)
                        item.UserProperties.Add("sync", Outlook.OlUserPropertyType.olText);
                    item.UserProperties["sync"].Value = "yes";
                    item.Save();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

        public void delete(String mail)
        {
            //MessageBox.Show(Properties.Settings.Default.List);
            lock (Properties.Settings.Default.List)
            {
                if (Properties.Settings.Default.List != "")
                {
                    HttpClient hc = new HttpClient();
                    try
                    {
                        hc.AuthedPost(mail, Properties.Settings.Default.List + "," + DateTime.Now.ToString());
                        Properties.Settings.Default.List = "";
                        Properties.Settings.Default.Save();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }
    }
}
